package com.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	
	private static final int CODE_COUNT=6;
	private static final String MOBILE = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
	private static final String EMAIL = "('')|(\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)";
	private static final String CARDID = "[1-9]{2}[0-9]{4}(19|20)[0-9]{2}"
            + "((0[1-9]{1})|(1[1-2]{1}))((0[1-9]{1})|([1-2]{1}[0-9]{1}|(3[0-1]{1})))"
            + "[0-9]{3}[0-9x]{1}";
	private static final String DRIVING_CODE="^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
	private static final String CHINACHAR="^[\u4E00-\u9FA5]{2,6}$";
	
	
	public static String formatString(String str){//格式化字符串
		if(isMobile(str)){
			return FormatMobile(str);
		}else if(isCardId(str)){
			return FormatCardId(str);
		}
		try{
			new RuntimeException("未知格式 : "+str);
		}catch(Exception e){
//			e.printStackTrace();
			return "未知格式";
		}
		return "未知格式";
	}

	public static boolean isFormatString(String str){//是否是需要被格式化的字符串
		if(str==null){
			return false;
		}else if(isCardId(str)){
			return true;
		}else if(isMobile(str)){
			return true;
		}
		return false;
	}
	
	public static boolean isEmail(String str){//判断邮箱号
		Pattern pattern = Pattern.compile(EMAIL);
		Matcher m=pattern.matcher(str);
		return m.matches();
	}
	public static boolean isMobile(String str){//判断手机号
		if(str==null){
			return false;
		}
		Pattern pattern = Pattern.compile(MOBILE);
		Matcher m=pattern.matcher(str);
		return m.matches();
	}
	public static boolean isCardId(String str){//判断身份证件
		if(str==null || str.length()!=18){
			return false;
		}
		Pattern pattern = Pattern.compile(CARDID);
		Matcher m=pattern.matcher(str);
		return m.matches();
	}
	public static boolean isDrivingCode(String str){//判断车牌号件
		if( str == null || str.length() != 7 ){
			return false;
		}
		Pattern pattern = Pattern.compile(DRIVING_CODE);
		Matcher m=pattern.matcher(str);
		return m.matches();
	}
	public static boolean isChinaCharName(String str){//是否是中文名字2-6位
		if(str.length() < 2 || str.length() > 6){
			return false;
		}
		return isChinaChar(str);
	}
	public static boolean isChinaChar(String str){//是否是中文
		if(str == null){
			return false;
		}
		Pattern pattern = Pattern.compile(CHINACHAR);
		Matcher m=pattern.matcher(str);
		return m.matches();
	}
	
	public static String formatMobile(String str){//格式化手机
		if(isMobile(str)){
			return FormatMobile(str);
		}
		try{
			new RuntimeException("手机号错误 :"+str);
		}catch(Exception e){
			
		}
		return str;
	}
	public static String formatCardId(String str){//格式化身份证件号
		if(isCardId(str)){
			return FormatCardId(str);
		}
		try{
			new RuntimeException("身份证件号号错误 : "+str);
		}catch(Exception e){
			
		}
		return str;
	}
	private static String FormatMobile(String str) {//格式化手机号
		StringBuilder sb=new StringBuilder();
		sb.append(str.substring(0, 3));
		sb.append("****");
		sb.append(str.substring(7, str.length()));
		return sb.toString();
	}
	private static String FormatCardId(String str) {//格式化身份证件号
		StringBuilder sb=new StringBuilder();
		sb.append(str.substring(0, 6));
		sb.append("********");
		sb.append(str.substring(14, str.length()));
		return sb.toString();
	}
	
	public static String getCodeByCount(){//得到默认验证码6个
		return getCode(CODE_COUNT);
	}
	
	public static String getCodeByCount(int count){//得到count位验证码
		if(count<4){
			count=CODE_COUNT;
		}
		return getCode(count);
	}
	
	private static String getCode(int count){//生成验证码
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<count;i++){//随机验证码
			int k=(int) Math.round(Math.random()*9);
			sb.append(k);
		}
		return sb.toString();
	}
	public static void main(String[] args) {
		System.out.println(isChinaCharName("我啊啊"));
	}
}
